<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addFormVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="归属" value="username"></el-option>
<!--              <el-option label="批次" value="batch"></el-option>-->
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>

    <!-- table -->
    <el-table stripe style="width: 100%" :data="tableData" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="140" align="center" prop="id" label="ID"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="140" align="center" prop="batch" label="批次">
        <template slot-scope="scope">
          <el-select v-show="scope.row.batchArr.length>0" v-model="scope.row.batchArr" placeholder="请选择" multiple disabled>
            <el-option
              v-for="item in scope.row.batchNameArr"
              :key="item.id"
              :label="item.batch"
              :value="item.id.toString()">
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="120" align="center" prop="admin_name" label="归属"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="110" align="center" prop="default_launcher" label="桌面app">
        <template slot-scope="scope">{{scope.row.app_name ? scope.row.app_name : '系统默认' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue" prop="create_time" label="创建时间" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.create_time| activateTime}}</template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="create_time" label="是否为系统默认" min-width="140" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.is_default==1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" min-width="150" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="editDefaultLauncher(scope.row)">编辑</el-button>
            <el-button size="mini" @click="deleteShow(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>

    <!-- 添加弹窗 -->
    <el-dialog title="系统默认Launcher" :visible.sync="addFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="25%" @close="addFormclose">
      <el-form :model="addForm" :rules="rules" ref="ruleForm" class="diolog_adapt">
        <el-form-item label="采用系统默认:" label-width="120px" prop="is_default">
          <el-radio-group v-model="addForm.is_default">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="-1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择批次" label-width="120px" prop="batch">
          <el-select v-model="addForm.batch" :multiple-limit="10" placeholder="请选择批次" style="flex:1" multiple filterable>
            <el-option v-for="item in lotoptions" :key="item.id" :label="item.batch" :value="item.id.toString()"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择桌面app" label-width="120px" prop="app_id" v-if="addForm.is_default==-1">
          <el-select v-model="addForm.app_id" placeholder="请选择桌面app" style="flex:1" >
            <el-option v-for="item in appOptions" :key="item.app_id" :label="item.app_name" :value="item.app_id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" size="mini" round @click="getaddDefaultApp('ruleForm')">确 定</el-button>
        <el-button v-else type="primary" @click="geteditDefaultApp('ruleForm')" size="mini" round>确定</el-button>
      </div>
    </el-dialog>
    <!--添加弹窗 -->

    <!-- 删除弹窗start -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
    <!-- 删除弹窗end -->
  </div>
</template>

<script>
  import UploadExcelComponent from "@/components/UploadExcelSn/index.vue";
  import dynamicTable from "@/components/DynamicTable";
  import { getBatchTwo } from "@/api/equipment.js";
  import { addDefaultLauncher } from "@/api/launcherMade.js";

  import deleteDialog from "@/components/deleteDialog";
  import tableMini from '@/utils/tableMini.js'
  import {deleteDefaultLauncher, editDefaultLauncher, queryDefaultLauncher} from "@/api/launcherMade";
  export default {
    mixins: [tableMini],
    components: { UploadExcelComponent, dynamicTable, deleteDialog },
    data() {
      return {
        checkboxVal: ["ID", "批次", "归属", "桌面app", "创建时间", "是否为系统默认"],
        tableData: [],
        select: "", //搜索条件
        //编辑按钮控制
        editSubjects: true,
        listQuery: { startSize: 1, pageSize: 10, batch: undefined, username: undefined, userId: localStorage.getItem("ID") },
        addFormVisible: false,
        lotoptions: [],
        appOptions: [],
        addForm: {
          userId: localStorage.getItem("ID"),
          last_ip: sessionStorage.getItem("Ip"),
          batch: [],
          app_id: '',
          is_default: 1, //是否采用系统默认launcher 【1：是 -1：否】
        },
        rules: { batch: [{ required: true, message: "请选择批次", trigger: "blur" }] }
      }
    },
    created() {
      this.getTabularData()
      this.getBatchTwos()
    },
    methods: {
      //删除按钮事件
      deleteEvents() {
        let params = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), id: this.deleteDialogs.deleteId }
        deleteDefaultLauncher(params).then(res => {
          this.$message.success(res.msg)
          this.getTabularData()
        }).catch(error => {
          this.getTabularData()
        })
      },
      //添加
      getaddDefaultApp(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addData = JSON.parse(JSON.stringify(this.addForm))
            addData.batch = this.addForm.batch.toString()
            addDefaultLauncher(addData).then(res => {
              this.$message.success(res.msg)
              this.getTabularData()
              this.addFormVisible = false
            }).catch(err => {
              this.getTabularData()
            })
          } else {
            return false;
          }
        });
      },
      //ID查询信息
      editDefaultLauncher(row) {
        this.addFormVisible = true
        this.editSubjects = false
        this.addForm.batch = row.batchArr
        this.addForm.app_id = row.app_id
        this.addForm.is_default = row.is_default
        this.addForm.id = row.id
      },
      //编辑信息
      geteditDefaultApp(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addForm.last_ip = sessionStorage.getItem("Ip")
            addForm.userId = localStorage.getItem("ID")
            for (let i in addForm) {
              addForm[i] = addForm[i] != null ? addForm[i].toString().replace(/^\s*|\s*$/g, "") : ''
            }
            editDefaultLauncher(addForm).then(res => {
              this.$message.success(res.msg)
              this.getTabularData()
              this.addFormVisible = false
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      //根据ID获取批次列表
      getBatchTwos() {
        let data = { userId: localStorage.getItem("ID") }
        getBatchTwo(data).then(res => {
          this.lotoptions = res.data
        }).catch(err => {
          this.lotoptions = []
        })
      },
      getTabularData() {
        queryDefaultLauncher(this.listQuery).then(res => {
          if(res.code == 200){
            this.tableData = res.data
            this.total = res.total
            this.appOptions = res.otherData['appArr'] != undefined ? res.otherData['appArr'] : []
          }else {
            this.$message.error(res.msg)
          }
        }).catch(err => {
          this.tableData = []
          this.total = 0
        })
      },
      //编辑添加表单关闭事件
      addFormclose() {
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields();
          this.editSubjects = true
          this.addForm = this.$options.data().addForm
        })
      }
    }
  }
</script>
